package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.AdminOrdersMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 星河一叶Ck
 * @date 2024/10/11
 */

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private AdminOrdersMapper adminOrdersMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 查询进入运营数据
     * @return
     */
    public BusinessDataVO getBusinessData(LocalDateTime begin,LocalDateTime end) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */
        // 存入到map中 map有什么然后查询什么
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);

        // 总订单数
        Integer totalOrderCount = adminOrdersMapper.countByMap(map);

        // 有效订单
        map.put("status", Orders.COMPLETED);
        Integer validOrderCount = adminOrdersMapper.countByMap(map);

        // 营业额
        Double turnover = adminOrdersMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;

        // 平均客单价
        Double AverageOrderValue = 0.0;

        // 订单完成率
        Double completedOrder = 0.0;
        if(totalOrderCount != 0 && validOrderCount != 0) {
            // 订单完成率
            completedOrder = validOrderCount.doubleValue() / totalOrderCount;
            // 平均客单价 = 营业额 / 有效订单数
            AverageOrderValue = turnover / validOrderCount.doubleValue();
        }

        // 新增用户
        Integer newUserNum = adminOrdersMapper.newUserCount(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(completedOrder)
                .unitPrice(AverageOrderValue)
                .newUsers(newUserNum)
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    public SetmealOverViewVO getSeatmealOverView() {
        // 查询启售或者停售的套餐数量
        Integer startNum = setmealMapper.getSetmealCountByStatus(1);
        Integer stopNum = setmealMapper.getSetmealCountByStatus(0);

        return SetmealOverViewVO.builder()
                .sold(startNum)
                .discontinued(stopNum)
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    public DishOverViewVO getDishOverView() {
        Integer startNum = dishMapper.getDishCountByStatus(1);
        Integer stopNum = dishMapper.getDishCountByStatus(0);

        return DishOverViewVO.builder()
                .sold(startNum)
                .discontinued(stopNum)
                .build();
    }

    /**
     * 查询今日订单数据
     * @param begin
     * @param end
     * @return
     */
    public OrderOverViewVO getOrderManageData(LocalDateTime begin, LocalDateTime end) {
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);

        // 待接单数量
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = adminOrdersMapper.countByMap(map);

        // 待派送数量
        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = adminOrdersMapper.countByMap(map);

        // 已完成数量
        map.put("status",Orders.DELIVERY_IN_PROGRESS);
        Integer completedOrders = adminOrdersMapper.countByMap(map);

        // 已取消数量
        map.put("status",Orders.CANCELLED);
        Integer cancelledOrders = adminOrdersMapper.countByMap(map);

        // 全部订单
        map.put("status",null);
        Integer allOrders = adminOrdersMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }


}
